package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.LogMapper;
import com.indusfo.spc.pojo.Log;
import com.indusfo.spc.service.LogService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 熊冰
 * @Date: 2020/1/6 14:56
 */
@Service
public class LogServiceImpl implements LogService {
	// 声明日志的静态变量
	private static final Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);

	@Resource
	private LogMapper logMapper;
	//查询
	@Override
	public JSONObject queryLog(Log log) {
		try {
			//获取页面的数据个数
			Integer pagesize = log.getPagesize();
			//获取查询的页数
			Integer pageindex = log.getPageindex();
			if(pagesize!=null && pageindex !=null){
				log.setIncept(pagesize*(pageindex-1));
			}
			//执行查询操作
			List<Log> logList=logMapper.queryLog(log);

			//如果页数和页面数量都不为空的情况下进行计数
			if(logList.isEmpty()){
				return  JSONObject.oK(HintMessage.QUERY_NULL,logList,0);
			}else{
				Integer count= logMapper.countLog(log);
				return JSONObject.oK(HintMessage.QUERY_SUCCESS,logList,count);
			}

		} catch (GlobalException e) {
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		}
	}
	//新增
	@Override
	public JSONObject insertLog(Log log) {
		try{
			if(log.getLogId()!=null){
				throw new ModifyFailedException(HintMessage.INSERT_ID);
			}
			//初始化基础数据
			if(log.getDataState()==null){
				log.setDataState(1);
			}

			//执行新增操作
			Integer row=logMapper.insertLog(log);
			if(row==0){
				throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
			}
			return JSONObject.oK(HintMessage.INSERT_SUCCESS);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}
	//修改
	@Override
	public JSONObject updateLog(Log log) {
		try {
			if(log.getLogId()==null) {
				throw new ParamsErrorException(HintMessage.UPDATE_ID);
			}
			//初始化基础数据
			if(log.getDataState()==null){
				log.setDataState(1);
			}

			//执行修改操作
			int row = logMapper.updateLog(log);
			if(row == 0) {
				throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
			}
			return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
		}catch (GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}
	//删除
	@Override
	public JSONObject deleteLog(List<Integer> logIds, Integer dataState) {
		try{
			//空值判断
			if(dataState == null) {
				throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
			}
			if(logIds == null) {
				throw new ParamsErrorException(HintMessage.LIST_NULL);
			}
			//记录异常信息
			String msg = "";
			switch (dataState) {
				case 1:
					msg = HintMessage.START_VALUE;break;
				case 2:
					msg = HintMessage.DELETE_VALUE;break;
				case 3:
					msg = HintMessage.STOP_VALUE;break;
				default:
					throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
			}
			//执行删除操作
			int row = logMapper.deleteLog(logIds,dataState);
			//声明msg字符串，用来存抛出的异常
			if (row == 0) {
				msg=msg+ HintMessage.DEFEATED_VALUE;
			} else {
				msg=msg+ HintMessage.SUCCESS_VALUE;
			}
			return JSONObject.oK(msg);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}



}

